GBML manual / Arrays

array_contains

True if any place in target holds a value equal to value, false otherwise.

Syntax

array_contains(target, value) -> bool

Arguments

NameType
targetarray
valueelement

Returns

bool

Description

true if any place in target, an array, holds a value equal to value, false otherwise.

A plain left-to-right search - target does not need to be sorted, and nothing about calling this changes target itself. Useful for "is this one of the allowed ones" checks - a key number that opens a door, a tile number that counts as water.

If you also want to know where the value is, array_find gives its place, or -1 if it is not there.

What counts as equal

Numbers are equal when they are the same number, and text is equal when every letter is the same. It is the same search list_contains makes, for the container that has a fixed number of places.

It looks at every place the array has

An array has exactly the places array_length says, and the search looks at them from the front until it finds a match. A longer array takes longer, and a search that finds nothing looks at every place.

In a script

A script that takes an array as a parameter works on the caller's own array (see Arrays). So array_contains inside a script searches the caller's array, whatever its size.

What happens in the awkward cases

You do thisWhat happens
search for a value that is not therefalse
search for a value that is there more than oncetrue
give it a listrefused when checked. A list has its own, list_contains

Example

var doors = array_create(3, 0);
doors[0] = 4;
doors[1] = 8;
doors[2] = 15;
var has8 = array_contains(doors, 8);     // true
var has99 = array_contains(doors, 99);   // false

On the GBA

A plain left-to-right search (find, the GBA module's own alias for std::find) - O(n) in how many places target has, same cost as writing the loop by hand. Fine for the small, bounded arrays this hardware ever holds; nothing about target needs to be sorted. It is exactly list_contains, for the container that has a fixed number of places.

See also: array_findlist_containsarray_lengtharray_copyArraysData containers